package summer.stackandqueue;

import java.util.LinkedList;

public class Solution150 {
    public int evalRPN(String[] tokens) {
        LinkedList<String> stack = new LinkedList<>();
        for (String s : tokens) {
            if (s.equals("+")) {
                int a = Integer.parseInt(stack.removeLast());
                int b = Integer.parseInt(stack.removeLast());
                stack.addLast(String.valueOf(a + b));
            } else if (s.equals("*")) {
                int a = Integer.parseInt(stack.removeLast());
                int b = Integer.parseInt(stack.removeLast());
                stack.addLast(String.valueOf(a * b));
            } else if (s.equals("-")) {
                int a = Integer.parseInt(stack.removeLast());
                int b = Integer.parseInt(stack.removeLast());
                stack.addLast(String.valueOf(b - a));
            } else if (s.equals("/")) {
                int a = Integer.parseInt(stack.removeLast());
                int b = Integer.parseInt(stack.removeLast());
                stack.addLast(String.valueOf(b / a));
            } else stack.addLast(s);
        }
        return Integer.parseInt(stack.removeLast());
    }
}
